package com.tanhua.dubbo.server.vo;

import com.tanhua.dubbo.server.pojo.UserLocation;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author why
 * 重新定义userLocation
 *  因为不能序列化 dubbo 使用的反射
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserLocationVo implements Serializable {

    private static final long serialVersionUID = 4133419501260037769L;

    private String id;
    private Long userId; //用户id
    private Double longitude; //经度
    private Double latitude; //维度
    private String address; //位置描述
    private Long created; //创建时间
    private Long updated; //更新时间
    private Long lastUpdated; //上次更新时间

    //定义一个方法构建userLocation 和 UserLocationVo 转化

    public static final UserLocationVo format(UserLocation userLocation){
        UserLocationVo userLocationVo = new UserLocationVo();
        userLocationVo.setAddress(userLocation.getAddress());
        userLocationVo.setCreated(userLocation.getCreated());
        userLocationVo.setId(userLocation.getId().toHexString());
        userLocationVo.setLastUpdated(userLocation.getLastUpdated());
        userLocationVo.setUpdated(userLocation.getUpdated());
        userLocationVo.setUserId(userLocation.getUserId());
        userLocationVo.setLongitude(userLocation.getLocation().getX());
        userLocationVo.setLatitude(userLocation.getLocation().getY());
        return userLocationVo;
    }

    //定义一个集合 用于userLocation 和 vo集合的转化

    public static final List<UserLocationVo> formatList(List<UserLocation> userLocationList){
        List<UserLocationVo> userLocationVos=new ArrayList<>();
        for (UserLocation userLocation : userLocationList) {
            userLocationVos.add(format(userLocation));
        }
        return userLocationVos;
    }
}
